package com.dxq.basic_ui_library;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author zsh
 * @date 2020/2/23.
 * 功能：自定义导航栏
 */

public class TabView extends FrameLayout {

    private View mView;
    private ImageView ivIcon;
    private ImageView ivIconPress;
    private TextView tvContent;

    private static int COLOR_DEFAULT = Color.GRAY;
    private static int COLOR_PRESS = Color.BLACK;

    public TabView(Context context) {
        super(context);
        init(context, null);
    }

    public TabView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public TabView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public TabView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    //初始化
    private void init(Context context, AttributeSet attrs) {
        if (attrs == null) return;
        //获取自定义属性
        //mView = LayoutInflater.from(context).inflate(R.layout.tab_view, this, false);
        inflate(context, R.layout.tab_view, this);
        ivIcon = (ImageView) findViewById(R.id.iv_icon);
        ivIconPress = (ImageView) findViewById(R.id.iv_icon_press);
        tvContent = (TextView) findViewById(R.id.tv_content);
        setProgress(0);
    }

    //设置icon、text
    //方法1：收取自定义属性，从xml设置
    //方法2：直接对外开放方法
    public void setIconAndText(int icon, int iconPress, String text) {
        ivIcon.setImageResource(icon);
        ivIconPress.setImageResource(iconPress);
        tvContent.setText(text);
    }

    //设置背景
    public void setProgress(float progress) {
        ivIcon.setAlpha(1 - progress);//透明度
        ivIconPress.setAlpha(progress);
        tvContent.setTextColor(evaluate(progress, COLOR_DEFAULT, COLOR_PRESS));
    }

    public int evaluate(float fraction, int startValue, int endValue) {
        int startInt = (Integer) startValue;
        int startA = (startInt >> 24) & 0xff;
        int startR = (startInt >> 16) & 0xff;
        int startG = (startInt >> 8) & 0xff;
        int startB = startInt & 0xff;

        int endInt = (Integer) endValue;
        int endA = (endInt >> 24) & 0xff;
        int endR = (endInt >> 16) & 0xff;
        int endG = (endInt >> 8) & 0xff;
        int endB = endInt & 0xff;

        return (int)((startA + (int)(fraction * (endA - startA))) << 24) |
                (int)((startR + (int)(fraction * (endR - startR))) << 16) |
                (int)((startG + (int)(fraction * (endG - startG))) << 8) |
                (int)((startB + (int)(fraction * (endB - startB))));
    }
}
